/*
 *  ProgressNode.fx -
 *  A custom node that functions as a progress bar
 *  TODO: Add the ability to have an "infinite progress" look as well
 *
 *  Developed 2008 by James L. Weaver (jim.weaver at lat-inc.com)
 *  to demonstrate how to create custom nodes in JavaFX
 *
 *  Used and changed 2008 by Jonathan ANTOINE to use in his videoPlayer
 */

package javafxarnestockmans;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;

public class ProgressNode extends CustomNode {

    /*
    * A number from 0.0 to 1.0 that indicates the amount of progress
     */
    public var progress:Number;

    /*
    * The fill of the progress part of the progress bar.  Because
     * this is of type Paint, a Color or gradient may be used.
     */
    public-init var progressFill:Paint =   LinearGradient {
        startX: 0.0
        startY: 0.0
        endX: 0.0
        endY: 1.0
        stops: [
            Stop {
                offset: 0.0
                color: Color.rgb(0, 192, 255)
            },
            Stop {
                offset: 0.20
                color: Color.rgb(0, 172, 234)
            },
            Stop {
                offset: 1.0
                color: Color.rgb(0, 112, 174)
            },
        ]
    };

    /*
    * The fill of the bar part of the progress bar. Because
     * this is of type Paint, a Color or gradient may be used.
     */
    public-init  var barFill:Paint = LinearGradient {
        startX: 0.0
        startY: 0.0
        endX: 0.0
        endY: 1.0
        stops: [
            Stop {
                offset: 0.0
                color: Color.rgb(112, 112, 112)
            },
            Stop {
                offset: 1.0
                color: Color.rgb(88, 88, 88)
            },
        ]
    };
    /*
    * The color of the progress percent text on the progress bar
     */
    public-init var progressPercentColor:Color = Color.rgb(191, 223, 239);

    /*
    * The color of the progress text on the right side of the progress bar
     */
    public-init var progressTextColor:Color = Color.WHITE;

    /*
    * Determines the width, in pixels, of the progress bar
     */
    public-init var width:Integer = 200;

    public var leftStr : String = "";
    public var rightStr : String = "";
    public var centerStr : String = "";

    /*
    * Determines the height, in pixels, of the progress bar
     */
    public-init var height:Integer = 20;

    /**
    * Create the Node
     */
    public override function create():Node {
        var textRef:Text;
        var progBarFont =Font.font("Verdana", FontWeight.EXTRA_BOLD, 10);
        var leftAndRightFont =Font.font("Sans serif", FontWeight.EXTRA_BOLD, 10);
        return Group{

            content: [
                // The entire progress bar
                Rectangle {
                    x:0
                    y:0
                    width: bind width
                    height: bind height
                    fill: bind barFill
                },
                Rectangle {
                    x:0
                    y:0
                    width: bind progress * width
                    height: height
                    fill: bind progressFill
                }
              ,
                // The percent complete displayed on the progress bar
                textRef = Text {
                    translateX: width / 2 - 20
                    translateY: 5
                    textOrigin: TextOrigin.TOP
                    font: progBarFont

                    fill: bind progressPercentColor
                    content: bind "{centerStr}({progress * 100 as Integer}%)"
                },
                Text {
                    translateX: bind width  - rightStr.length() * 6
                    translateY: 5
                    textOrigin: TextOrigin.TOP
                    font: leftAndRightFont
                    fill: bind progressPercentColor
                    content: bind "{rightStr}"
                },
                Text {
                    translateX: 4
                    translateY: 5
                    textOrigin: TextOrigin.TOP
                    font: leftAndRightFont
                    fill: bind progressPercentColor
                    content: bind "{leftStr}"
                }

            ]
        }

    }


}